<?php
namespace app\houtai\Controller;
use app\houtai\controller\Base;
use app\houtai\controller\Tree;
use think\Controller;
use think\Db;
use think\Request;
use think\File;
class Thumbpic extends Base
{
    public function lst()
    {   
        $datat =db('cate')->select();
        $res = Tree::toList($datat); 
        $this->assign('Thumbpic',$res);
        if(Request::instance()->isPost()){
            if(input('cid')>0){
                $data = db('thumb')->where('title','like','%'.input('keywords').'%')->where('cateid',input('cid'))->order('id desc')->paginate(8);
            }else{
                $data = db('thumb')->where('title','like','%'.input('keywords').'%')->order('id desc')->paginate(8);
            }
        }else{
            $data = db('thumb')->order('id desc')->paginate(8);
        }
        $this->assign('thumb',$data);
        return view();
    }
    public function uploadimg()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('id_photos');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            // 成功上传后 获取上传信息
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            db('annx')->data([
                'sctime'=>time(),//更新时间
                'picsrc'=>YM . str_replace("\\", "/", $info->getSaveName()),//图片地址
                'prname'=>'未知',//所属栏目
                'picname'=>$info->getFilename(),//图片名称                
                ])->insert();
                //插入栏目图片
            return json_encode([
                'status'=>1,
                'filename'=> YM . str_replace("\\", "/", $info->getSaveName()),
            ]);
        }
    }
    public function add()
    {
        $datat =db('cate')->select();
        $this->assign('Thumbpic',$datat);
        $res = Tree::toList($datat); 
        $this->assign('Thumbpic',$res);
        return view();
    }
    public function addajax()
    {
        $data=$_POST;
        $data['time']=time();
        $data['newtime']=time();
        if ($data['thumb']==null) {
            srand( microtime() * 1000000 );
            $num = rand( 1, 4 );

            switch( $num )
            {
                case 1: $image_file = DOM . "public/static/index/images/timg.jpg";
                    break;
                case 2: $image_file = DOM . "public/static/index/images/timg1.jpg";
                    break;
                case 3: $image_file = DOM . "public/static/index/images/timg2.jpg";
                    break;
                case 4: $image_file = DOM . "public/static/index/images/timg3.jpg";
                    break;
            }
            $data['thumb'] = $image_file;
        }//当没有上传封面图时，随机一张本地已有的图片
        preg_match_all('#src="([^"]+?)"#',$data['content'] , $img);//获取文本中的图片
        foreach ($img[1] as $value) {
            $picname= explode('/',$value);
            for ($i=0; $i < count($picname) ; $i++) { 
                $name=$picname[$i];
            }
            db('annx')->data(['sctime'=>$data['time'],'picsrc'=>$value,'picname'=>$name])->insert();
        }
        $res=db('thumb')->insert($data);
        $articleId = Db::name('thumb')->getLastInsID();
        $data['url']= DOM.'index.php/index/thumb/show/cateid/'.$data['cateid'].'/id/'.$articleId;
        $data['id']=$articleId;
        db('thumb')->update($data);
        if ($res) {
            return json_encode([
                'status'=>1,
                ]);
        }else{
            return json_encode([
                'status'=>0,
                ]);
        }
    }
    public function edit()
    {
        $id=input('id');
        if($id!=0){
            // $id=input('id');
            $data=db('thumb')->where('id',$id)->find();
            $datas=db('cate')->select();
            $this->assign('thumbedit',$data);
            $res = Tree::toList($datas); 
            $this->assign('cate',$res);
            return view();
        }
        return view();
    }
    public function editajax()
    {
        $data = $_POST;
        $data['newtime']=time();
        if ($data['thumb']==null) {
            srand( microtime() * 1000000 );
            $num = rand( 1, 4 );

            switch( $num )
            {
                case 1: $image_file = DOM . "public/static/index/images/timg.jpg";
                    break;
                case 2: $image_file = DOM . "public/static/index/images/timg1.jpg";
                    break;
                case 3: $image_file = DOM . "public/static/index/images/timg2.jpg";
                    break;
                case 4: $image_file = DOM . "public/static/index/images/timg3.jpg";
                    break;
            }
            $data['thumb'] = $image_file;
        }//当没有上传封面图时，随机一张本地已有的图片
        preg_match_all('#src="([^"]+?)"#',$data['content'] , $img);//获取文本中的图片
        foreach ($img[1] as $value) {
            $picname= explode('/',$value);
            for ($i=0; $i < count($picname) ; $i++) { 
                $name=$picname[$i];
            }
            if (!db('annx')->where('picname','like',$name)->find()) {
                db('annx')->data(['sctime'=>$data['newtime'],'picsrc'=>$value,'picname'=>$name])->insert();
            }
            //dump(db('annx')->where('picname','like',$name)->find()); die;
            
        }
        $data['url']= DOM.'index.php/index/thumb/show/cateid/'.$data['cateid'].'/id/'.$data['id'];
        $res=db('thumb')->update($data);
        if ($res) {
            return json_encode([
                'status'=>1,
                ]);
        }else{
            return json_encode([
                'status'=>0,
                ]);

        }

        dump($data); die;
        return view();
    }
    public function del()
    {
        $id=input('id');
        $datas=db('thumb')->where('id',$id)->find();
        $res=db('thumb')->delete($id);
        if($res){
            return json_encode([
                'status'=>1,
                ]);
        }else{
            return json_encode([
                'status'=>0,
                ]);
        }
    }
    // 单个删除
    public function delall()
    {
        $id=input('id');
        $datas=db('thumb')->where('id',$id)->find();
        $res=db('thumb')->delete($id);
        if($res){
            return json_encode([
                'status'=>1,
                ]);
        }else{
             return json_encode([
                'status'=>0,
                ]);
        }
    }
    // 全删
    
}
